package com.wenya.counter.ui.main;

import androidx.appcompat.app.AlertDialog;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wenya.counter.R;
import com.wenya.counter.data.bean.ScriptureBean;
import com.wenya.counter.databinding.MainFragmentBinding;
import com.wenya.counter.ui.add.AddActivity;

public class MainFragment extends Fragment {

    private MainViewModel mViewModel;

    private MainFragmentBinding viewDataBinding;

    public static MainFragment newInstance() {
        return new MainFragment();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        viewDataBinding = DataBindingUtil.inflate(inflater, R.layout.main_fragment, container, false);
        return viewDataBinding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(MainViewModel.class);
        getLifecycle().addObserver(mViewModel);
        initView();
    }

    @SuppressLint("StringFormatMatches")
    private void initView() {
        ScriptureAdapter scriptureAdapter = new ScriptureAdapter(mViewModel);
        viewDataBinding.listView.setLayoutManager(new LinearLayoutManager(getContext()));
        viewDataBinding.listView.setAdapter(scriptureAdapter);
        mViewModel.dataList.observe(getViewLifecycleOwner(), dataList -> {
            if (viewDataBinding.checkAll.isChecked() && dataList != null) {
                for (ScriptureBean item : dataList) {
                    item.setChecked(true);
                }
            }
            scriptureAdapter.setData(dataList);
        });
        mViewModel.houseNumber.observe(getViewLifecycleOwner(), houseNumber -> {
            viewDataBinding.totalInfo.setText(getString(R.string.house_number, houseNumber));
        });
        viewDataBinding.more.setOnClickListener(v -> {
            MenuPop menu = new MenuPop(getActivity());
            menu.showAsDropDown(v, 0, v.getY() - 30);
            menu.setListener(new MenuPop.Listener() {
                @Override
                public void add() {
                    startActivityForResult(new Intent(getActivity(), AddActivity.class), 1000);
                }

                @Override
                public void edita() {
                    viewDataBinding.editGroup.setVisibility(View.VISIBLE);
                    scriptureAdapter.setEdita(true);
                }
            });
        });
        viewDataBinding.tvCancel.setOnClickListener(view -> {
            cancelEdita(scriptureAdapter);
        });
        viewDataBinding.checkAll.setOnCheckedChangeListener((buttonView, isChecked) -> {
            scriptureAdapter.checkAll(isChecked);
        });
        viewDataBinding.tvReset.setOnClickListener(view -> {
            AlertDialog alertDialog = new AlertDialog.Builder(getActivity())
                    .setTitle(R.string.tips)
                    .setMessage(R.string.resetTips)
                    .setPositiveButton(R.string.ok, (dialog, which) -> {
                        mViewModel.resetData();
                        cancelEdita(scriptureAdapter);
                        mViewModel.loadData();
                    })
                    .setNegativeButton(R.string.cancel, null)
                    .create();
            alertDialog.show();

        });
        viewDataBinding.tvDelete.setOnClickListener(view -> {
            AlertDialog alertDialog = new AlertDialog.Builder(getActivity())
                    .setTitle(R.string.tips)
                    .setMessage(R.string.deleteTips)
                    .setPositiveButton(R.string.ok, (dialog, which) -> {
                        mViewModel.delete();
                        cancelEdita(scriptureAdapter);
                        mViewModel.loadData();
                    })
                    .setNegativeButton(R.string.cancel, null)
                    .create();
            alertDialog.show();

        });
    }

    private void cancelEdita(ScriptureAdapter scriptureAdapter) {
        viewDataBinding.editGroup.setVisibility(View.GONE);
        scriptureAdapter.setEdita(false);
        viewDataBinding.checkAll.setChecked(false);
    }


    @Override
    public void onDestroyView() {
        getLifecycle().removeObserver(mViewModel);
        super.onDestroyView();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1000) {
            mViewModel.loadData();
        }
    }
}